<?php
class MaucungctsController extends AppController {

	var $name = 'Maucungcts';

	function index($idmau) {
		$this->Maucungct->recursive = 0;
		$this->Cookie->write('idmau',$idmau);
		$this->set('maucungcts', $this->paginate('Maucungct',array('Maucungct.maucung_id'=>$idmau)));
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid maucungct', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('maucungct', $this->Maucungct->read(null, $id));
	}

	function add() {
		$idmau=$this->Cookie->read('idmau');
		if (!empty($this->data)) {
			$this->data ['Maucungct'] ['maucung_id']=$idmau;
			$this->Maucungct->create();
			if ($this->Maucungct->save($this->data)) {
				$this->Session->setFlash(__('The maucungct has been saved', true));
				$this->redirect(array('action' => 'index',$idmau));
			} else {
				$this->Session->setFlash(__('The maucungct could not be saved. Please, try again.', true));
			}
		}
		$idchiphi_daco=$this->Maucungct->find('list',array(
			'conditions'=>array('maucung_id'=>$idmau),
			'fields'=>array('chiphi_id','chiphi_id')));

		$chiphis = $this->Maucungct->Chiphi->find('list',
			array('conditions'=>array('NOT'=>array('id'=>$idchiphi_daco),'length(nhoi)'=>10),
				'fields'=>array('id','tencp')));
		$this->set(compact('maucungs', 'chiphis','idmau'));
	}

	function delete($id = null) {
		$idmau=$this->Cookie->read('idmau');
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for maucungct', true));
			$this->redirect(array('action'=>'index',$idmau));
		}
		if ($this->Maucungct->delete($id)) {
			$this->Session->setFlash(__('Maucungct deleted', true));
			$this->redirect(array('action'=>'index',$idmau));
		}
		$this->Session->setFlash(__('Maucungct was not deleted', true));
		$this->redirect(array('action' => 'index',$idmau));
	}
}
